package com.ayuku.commons;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public abstract class FormDialogSetting extends org.eclipse.swt.widgets.Dialog {
	private Shell dialogShell;
	public Group grpData;
	private Button btnSave;
	private Composite composite1;
	private String funcId;

	public String getFuncId() {
		return funcId;
	}

	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}

	public FormDialogSetting(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);
		

			FormLayout dialogShellLayout = new FormLayout();
			dialogShell.setBackground(SWTResourceManager.getColor(215, 213, 253));
			dialogShell.setLayout(dialogShellLayout);
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setSize(610, 384);
			{
				FormData composite1LData = new FormData();
				composite1LData.width = 80;
				composite1LData.height = 30;
				composite1LData.right =  new FormAttachment(1000, 1000, -7);
				composite1LData.bottom =  new FormAttachment(1000, 1000, -5);
				composite1 = new Composite(dialogShell, SWT.NONE);
				FillLayout composite1Layout = new FillLayout(
						org.eclipse.swt.SWT.HORIZONTAL);
				composite1.setLayout(composite1Layout);
				composite1.setLayoutData(composite1LData);
				{
					btnSave = new Button(composite1, SWT.PUSH | SWT.CENTER);
					btnSave.setImage(SWTResourceManager.getImage(FormDialogSetting.class, "/images/form/database_save.png"));
					btnSave.setAlignment(SWT.DOWN);
					btnSave.setText("  &Save");
					btnSave.setToolTipText("Save data");
					btnSave.setCursor(SWTResourceManager
							.getCursor(SWT.CURSOR_HAND));
					btnSave.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							btnSaveWidgetSelected(evt);
						}
					});
				}
			}

			{
				grpData = new Group(dialogShell, SWT.NONE);

				FillLayout group1Layout = new FillLayout(
						org.eclipse.swt.SWT.HORIZONTAL);
				grpData.setLayout(group1Layout);

				FormData grpDataLData = new FormData();
				grpDataLData.width = 582;
				grpDataLData.height = 285;
				grpDataLData.left = new FormAttachment(0, 1000, 7);
				grpDataLData.top = new FormAttachment(0, 1000, 8);
				grpDataLData.right = new FormAttachment(1000, 1000, -7);
				grpDataLData.bottom = new FormAttachment(1000, 1000, -41);
				grpData.setLayoutData(grpDataLData);
				getPanelContent(grpData);
			}

			additionalProses();
			grpData.setBackground(SWTResourceManager.getColor(215, 213, 253));
			dialogShell.open();
			loadData();

			Display display = dialogShell.getDisplay();

			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public final void loadData() {
		doLoad();

	}

	public final void saveData() {
		try {
			if (doSave()) {
				Util.showMsgBox(AYConst.TRX_SUCCESS, 0, getDialogShell());
				getDialogShell().dispose();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Util.showMsgBox(AYConst.TRX_FAIL, 0, getDialogShell());
			getDialogShell().dispose();
		}

	}

	private final void btnSaveWidgetSelected(SelectionEvent evt) {
		saveData();
	}

	public abstract void getPanelContent(Group grpData);

	public abstract void additionalProses();

	public abstract boolean doSave() throws Exception;

	public abstract void doLoad();

	public final Shell getDialogShell() {
		return dialogShell;
	}

	public final void setDialogShell(Shell dialogShell) {
		this.dialogShell = dialogShell;
	}

	public Group getGrpData() {
		return grpData;
	}

	public void setGrpData(Group grpData) {
		this.grpData = grpData;
	
	}

	public final void refreshData() {
		doLoad();
	}

	/**
	 * @return the btnSave
	 */
	public Button getBtnSave() {
		return btnSave;
	}

	/**
	 * @param btnSave
	 *            the btnSave to set
	 */
	public void setBtnSave(Button btnSave) {
		this.btnSave = btnSave;
		btnSave.setBackground(SWTResourceManager.getColor(215, 213, 253));
	}
}
